package com.xx.pmis.web.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xx.pmis.model.Present;
import com.xx.pmis.model.Shop;
import com.xx.pmis.model.util.Chart;
import com.xx.pmis.services.PresentService;
import com.xx.pmis.services.ShopService;

public class ChartPresent extends PMISBaseUtil {
	private static final Log log = LogFactory.getLog(ChartPresent.class);

	ShopService shopService = getPMISServices().getShopService();
	PresentService presentService = getPMISServices().getPresentService();

	// 组装饼图数据集
	public List<Chart> buildPieDataset() {
		List<Chart> listChart = new ArrayList<Chart>();
		List<?> listShop = null;
		try {
			listShop = shopService.getAllShop();
		} catch (Exception e) {
			log.info("组装饼图数据集时，查询所有商店异常！" + e.toString());
		}
		Iterator<?> itShop = listShop.iterator();
		double temp = 0.00;
		while (itShop.hasNext()) {
			// 饼图实体
			Chart chart = new Chart();
			double total = 0.00;
			Shop shop = (Shop) itShop.next();
			List<?> listPresent = null;
			try {
				listPresent = presentService.getAllPresentByCondition(" where shop.shopID = '" + shop.getShopID() + "'");
			} catch (Exception e) {
				log.info("组装饼图数据集时，根据商店ID查询礼品异常！" + e.toString());
			}
			Iterator<?> itPresent = listPresent.iterator();
			while (itPresent.hasNext()) {
				Present present = (Present) itPresent.next();
				total = total + Double.parseDouble(present.getPresentExpenses());
			}
			if(total > 1000.00) {
				chart.setTotal(total);
				chart.setParam1(shop.getShopName());
				listChart.add(chart);
			} else {
				temp = temp + total;
			}
		}
		// 饼图实体
		Chart chartTemp = new Chart();
		chartTemp.setTotal(temp);
		chartTemp.setParam1("其他");
		listChart.add(chartTemp);

		return listChart;
	}

	// 组装柱状图数据集
	public List<Chart> buildCategoryDataset() {
		List<Chart> listChart = new ArrayList<Chart>();
		for (int j = 2; j >= 0; j--) {
			int y = 0;
			String year = CurrentDate.currentYear();
			y = Integer.parseInt(year) - j;
			for (int i = 1; i <= 12; i++) {
				// 柱状图实体
				Chart chart = new Chart();
				double total = 0.00;
				List<?> listPresent = null;
				try {
					listPresent = presentService.getAllPresentByCondition("where year(presentDate)='" + String.valueOf(y)
									+ "' and month(presentDate)='" + String.valueOf(i) + "'");
				} catch (Exception e) {
					log.info("组装饼图数据集时，根据商店ID查询礼品异常！" + e.toString());
				}
				Iterator<?> itPresent = listPresent.iterator();
				while (itPresent.hasNext()) {
					Present present = (Present) itPresent.next();
					total = total + Double.parseDouble(present.getPresentExpenses());
				}
				chart.setTotal(total);
				chart.setParam1(String.valueOf(i) + "月");
				chart.setParam2(String.valueOf(y) + "年");
				listChart.add(chart);
			}
		}
		return listChart;
	}
}
